package com.example.rxjava.extend

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.text.Editable
import android.text.TextWatcher
import android.view.Gravity
import android.view.View
import android.widget.EditText
import android.widget.Toast

fun View.setRoundBackground(backgroundColor: Int = Color.WHITE, radiu: Float = 0f, strokeColor: Int = Color.TRANSPARENT, strokeWidth: Int = 0) {
    background = GradientDrawable().apply {
        setBackgroundColor(backgroundColor)
        setStroke(strokeWidth, strokeColor)
        cornerRadius = radiu
    }
}

fun Context.toast(msg: String) {
    val toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT)
    toast.setGravity(Gravity.CENTER, 0, 0)
    toast.show()
}

fun EditText.onTextChange(afterTextChanged: (s: Editable?) -> Unit) {
    this.addTextChangedListener(object: TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            afterTextChanged.invoke(s)
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

    })
}